﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AutomatedDatabaseDR;
using System.Configuration;


namespace SqlServerDRControlPanel
{

    public partial class ManageApplications : Form
    {

        private DrType LocalDrType = DrType.DatabaseMirroring;



        public ManageApplications()
        {
            InitializeComponent();
            GetEditAppData();
        }
        #region Add Server
        private void Button_AddServer_Click(object sender, EventArgs e)
        {
            string principleServer = this.Tb_PrincipleSvr.Text;
            string mirrorServer = this.Tb_MirrSvr.Text;
            string databaseName = this.Tb_DbName.Text;

            if (principleServer == "Principle Server")
            {
                MessageBox.Show("enter data");
                return;
            }

            string ConfigServer = ConfigurationManager.AppSettings["ConfigDbConn"];
            string[] ConfigItems = ConfigServer.Split('|');
            ConfigItems ci = new ConfigItems(ConfigItems[0], ConfigItems[1]);

            try
            {
                ci.InsertConfigData(principleServer, mirrorServer, databaseName, LocalDrType);
                string message = "Insert Success";
                MessageBox.Show("Insert Complete", message, MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Insert Error", MessageBoxButtons.OK);
            }
        }

        private void textBoxGeneral_Enter(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;

            switch (tb.Text)
            {
                case "Principle Server":
                    tb.Text = "";
                    break;
                case "Database Name":
                    tb.Text = "";
                    break;
                case "Mirror Server":
                    tb.Text = "";
                    break;
            }
            tb.ForeColor = Color.Black;
        }

        private void textBoxGeneral_Leave(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (tb.Text == "")
            {
                tb.ForeColor = Color.Gray;
                switch (tb.Name)
                {
                    case "Tb_PrincipleSvr":
                        tb.Text = "Principle Server";
                        break;
                    case "Tb_DbName":
                        tb.Text = "Database Name";
                        break;
                    case "Tb_MirrSvr":
                        tb.Text = "Mirror Server";
                        break;
                }
            }

        }

        private void rbdrType_Mirroring_CheckedChanged(object sender, EventArgs e)
        {
            LocalDrType = DrType.DatabaseMirroring;
        }

        private void rbdrType_LogShipping_CheckedChanged(object sender, EventArgs e)
        {
            LocalDrType = DrType.LogShipping;
        }

        private void rbdrType_Replication_CheckedChanged(object sender, EventArgs e)
        {
            LocalDrType = DrType.Replication;
        }

        private void rbdrType_Other_CheckedChanged(object sender, EventArgs e)
        {
            LocalDrType = DrType.Other;
        }

        private void EditTabEntered(object sender, EventArgs e)
        {
            MessageBox.Show("edit entered");
        }
        #endregion


        private void GetEditAppData()
        {
            string ConfigServer = ConfigurationManager.AppSettings["ConfigDbConn"];
            string[] ConfigItems = ConfigServer.Split('|');
            ConfigItems ci = new ConfigItems(ConfigItems[0], ConfigItems[1]);

            try
            {
                dataGridView_Edit.AutoGenerateColumns = true;
                dataGridView_Edit.DataSource = ci.RestoreList(ci.ServerList().Tables[0]);

                

            }
            catch (System.Data.SqlClient.SqlException se)
            {
                if (se.ToString().Contains("An error has occurred while establishing a connection to the server."))
                {
                    MessageBox.Show("Unable to connect to server\r\nCheck the application.config file.", "Failure getting config data");
                    Application.Exit();
                }
                else
                {
                    MessageBox.Show(se.Message, "Failure getting config data");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source, "Failure getting config data");
            }
        }

        private void DeleteSelected_btn_Click(object sender, EventArgs e)
        {
            string ConfigServer = ConfigurationManager.AppSettings["ConfigDbConn"];
            string[] ConfigItems = ConfigServer.Split('|');
            ConfigItems ci = new ConfigItems(ConfigItems[0], ConfigItems[1]);

            for (int i = 0; i < dataGridView_Edit.RowCount; i++)
            {
                bool cbstate1 = false;
                Object cbstate = this.dataGridView_Edit.Rows[i].Cells[0].Value;
                string databaseName = dataGridView_Edit.Rows[i].Cells[2].Value.ToString();

                if (cbstate == null)
                {
                    cbstate1 = false;
                }
                else
                {
                    cbstate1 = Convert.ToBoolean(cbstate);
                }


                if (cbstate1)
                {
                    
                    try
                    {
                        ci.DeleteConfigItems(databaseName);
                        MessageBox.Show("Item Deleted");
                        // todo reload grid data

                    }
                    catch (Exception ex)
                    {
                        string errorString = string.Format("Error Deleting {0}\r\n{1}", databaseName, ex.Message);
                        MessageBox.Show(errorString);
                    }
                }
            }
        }

        private void AddPostAction_btn_Click(object sender, EventArgs e)
        {
            string databaseName = "";
            bool cbstate1 = false;
            for (int i = 0; i < dataGridView_Edit.RowCount; i++)
            {
                Object cbstate = this.dataGridView_Edit.Rows[i].Cells[0].Value;
                databaseName = dataGridView_Edit.Rows[i].Cells[1].Value.ToString();

                if (cbstate == null)
                {
                    cbstate1 = false;
                }
                else
                {
                    cbstate1 = Convert.ToBoolean(cbstate);
                    databaseName = dataGridView_Edit.Rows[i].Cells[1].Value.ToString();
                    break;
                }
            }

            if (cbstate1)
            {
                AddPostScript ps = new AddPostScript(databaseName);
                ps.Visible = true;
                ps.Activate();
            }

           
        }

    }
}
